package org.jeecg.modules.lab.pageOffice;


import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.lab.util.CsbcbhUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.Map;

import static com.aliyun.oss.internal.RequestParameters.ENCODING_TYPE;

/**
 * @Author: dong
 * @Date: 2022/3/11 16:50
 * @Version 1.0
 */
@RestController
@Api(tags = "pageOffice")
@RequestMapping(value = "/SimpleWord")
public class SimpleWordController {
    //获取doc目录的磁盘路径
//    private String dir = GetDirPathUtil.getDirPath() + "static/doc/";

    //文件路径
    @Autowired
    private CsbcbhUrl csbcbhUrl;

    @RequestMapping(value = "/Word")
    @ResponseBody
    public String showWord(HttpServletRequest request) throws UnsupportedEncodingException {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
//        String filePath = request.getParameter("filePath");
//        String file = request.getParameter("file");
//        String encodePath = URLEncoder.encode(filePath.replace("//", "\\"), "utf-8");
        String encodePath = URLEncoder.encode("D:\\zhuiExperiment\\downloadPath\\".replace("//", "\\"), "utf-8");
        poCtrl.setSaveFilePage("/SimpleWord/save?path=" + encodePath);
        //打开word
//        poCtrl.webOpen(file.replace("//", "\\"), OpenModeType.docNormalEdit, "张三");
        poCtrl.webOpen("D:\\zhuiExperiment\\downloadPath\\1701747178911pH值.docx", OpenModeType.docNormalEdit, "张三");
        return poCtrl.getHtmlCode();
    }

    @RequestMapping(value = "/Word1")
    @ResponseBody
    public Result<?> showWord1(@RequestBody Map map, HttpServletRequest request) throws UnsupportedEncodingException {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        //获取文件的名字
        String fileName=(String) map.get("entrustType")+"-"+(String) map.get("reportCode");
//        System.out.println("fileName:"+fileName);
        String userName = (String) map.get("userName");
//        File folder = new File(MyEnum.downPath);
        String path=MyEnum.downPath+"\\"+fileName+".docx";
//        System.out.println("path:"+path);
//        File[] files = folder.listFiles();
//        String path="";
//        if (files != null) {
//            for (File file : files) {
//                if (file.isFile() && file.getName().startsWith(fileName)) {
//                    path=file.getAbsolutePath();
//                    System.out.println("找到文件：" + file.getAbsolutePath());
//                }
//            }
//        }
        //添加自定义按钮
//        poCtrl.addCustomToolButton("保存", "Save", 1);
        //设置保存页面
        //将参数编码，否则会报错
        String encodePath = URLEncoder.encode(MyEnum.downPath, "utf-8");
        poCtrl.setSaveFilePage("/SimpleWord/save?path=" + encodePath);//设置处理文件保存的请求方法
        //打开Word文档
          //todo：切换返回的url
        if (!"".equals(path)) {
            poCtrl.webOpen(path, OpenModeType.docRevisionOnly, userName);
            return Result.OK("成功", poCtrl.getHtmlCode());
        }else {
            return Result.error("未找到文件", poCtrl.getHtmlCode());
        }
    }

    @RequestMapping(value = "/fileName")
    @ResponseBody
    public Result<?> fileName(@RequestBody Map map, HttpServletRequest request) throws UnsupportedEncodingException {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        String reportNumber = (String) map.get("reportNumber");
        String userName = (String) map.get("userName");
        String fileName = (String) map.get("fileName");
        String parentPath = (String) map.get("parentPath");

        //文件所在路径
        String path = "";
        //文件全路径
        String fileAbsolutePath = "";
        //该文件是报告路径下的
        if (new File(csbcbhUrl.getPageOffice() + reportNumber + "\\" + fileName).exists()) {
            path = csbcbhUrl.getPageOffice() + reportNumber + "\\";
            fileAbsolutePath = path + fileName;
        }
        //该文件是附件路径下的
        else if (new File(csbcbhUrl.getAttachment() + reportNumber + "\\" + parentPath + "\\" + fileName).exists()) {
            path = csbcbhUrl.getAttachment() + reportNumber + "\\" + parentPath + "\\";
            fileAbsolutePath = path + fileName;
        }
        //该文件是上传的路径下的
        else if (new File(csbcbhUrl.getUploadpath() + fileName).exists()) {
            path = csbcbhUrl.getUploadpath();
            fileAbsolutePath = path + fileName;
        }
        //打开模式
        OpenModeType mode = null;
        if (fileAbsolutePath.contains(".pdf")) {
            return Result.OK("成功", fileAbsolutePath);
        } else if (fileAbsolutePath.contains(".doc")) {
            mode = OpenModeType.docRevisionOnly;
        } else if (fileAbsolutePath.contains(".xl")) {
            mode = OpenModeType.xlsReadOnly;
        }

        String encodePath = URLEncoder.encode(path, "utf-8");
        poCtrl.setSaveFilePage("/SimpleWord/save?path=" + encodePath);//设置处理文件保存的请求方法
        //打开Word文档
//        poCtrl.webOpen(fileAbsolutePath, OpenModeType.docRevisionOnly, userName);
        poCtrl.webOpen(fileAbsolutePath, mode, userName);
        return Result.OK("成功", poCtrl.getHtmlCode());
    }


    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response, @RequestParam String path) throws UnsupportedEncodingException {
        String decodePath = URLDecoder.decode(path, "utf-8");
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile(decodePath + "\\" + fs.getFileName());
        fs.close();
    }


}

